package com.csi.service.impl;

import com.csi.domain.Power;
import com.csi.domain.Role;
import com.csi.domain.RolePower;
import com.csi.mapper.RoleMapper;
import com.csi.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    /**
     * 新增角色
     * @param roleName 角色名称
     * @param hotelId 酒店id
     * @return
     */
    @Override
    public int save(Role role) {
        int saved = roleMapper.save(role);
        int saved2 = roleMapper.saveRolePowers(role.getId(),role.getPowers());
        return saved2 + saved;
//        return roleMapper.save(roleName,hotelId);
    }

    /**
     * 根据角色名和酒店id查找角色
     * @param roleName
     * @param hotelId
     * @return
     */
    @Override
    public Role findByName(String roleName, long hotelId) {
        return roleMapper.findByName(roleName,hotelId);
    }

    /**
     * 根据角色id查找角色
     * @param roleId 角色id
     * @return
     */
    @Override
    public Role findRoleById(long roleId) {
        return roleMapper.findRoleById(roleId);
    }

    /**
     * 更新角色
     * @return
     */
    @Override
    @Transactional
    public int updateRole(Role role) {
        int updateds =  roleMapper.updateRoleInfo(role);
        int byId = roleMapper.deleteRolePowerById(role.getId());
        int updated = roleMapper.updateRoleInfo(role);
        List<RolePower> rolePowerList = new ArrayList<>();
        for (long powerId:role.getPowers()){
            RolePower rolePower = new RolePower();
            rolePower.setRoleId(role.getId());
            rolePower.setPowerId(powerId);
            rolePowerList.add(rolePower);
        }

        int added =  roleMapper.saveRolePower(rolePowerList);
        System.out.println(byId + updated + added);
        return byId + updated + updateds;
    }

    /**
     * 是否为初始管理员
     * @param roleId
     * @return
     */
    @Override
    public boolean isInitialAdmin(long roleId,long hotelId) {
        return roleMapper.isInitialAdmin(roleId,hotelId);
    }

    /**
     * 查找角色
     * @return List<Role> 角色集合
     */
    @Override
    public List<Role> findRole(long hotelId) {
        return roleMapper.findRole(hotelId);
    }
    /**
     * 通过角色id查找角色权限
     * @return List<Power>
     */
    @Override
    public List<Power> findPowerById(long roleId) {
        return roleMapper.findPowerById(roleId);
    }
    /**
     * 查找所有角色权限
     * @return List<Power>
     */
    @Override
    public List<Power> findAllPower() {
        return roleMapper.findAllPower();
    }
    /**
     * 通过角色id删除角色权限
     * @return List<Power>
     */
    @Transactional
    @Override
    public int deleteRoleById(long roleId) {
        int deleted = roleMapper.deleteRole(roleId);
        int deleted2 = roleMapper.deleteRolePowerById(roleId);
        return deleted + deleted2;
    }
    /**
     * 更新用户权限
     * @param role 用户权限
     * @return 条数
     */
    @Transactional
    @Override
    public int updateRolePower(Role role) {
        int byId = roleMapper.deleteRolePowerById(role.getId());
        int updated = roleMapper.updateRoleInfo(role);
        for (RolePower rolePower:role.getRolePowerList()){
            rolePower.setRoleId(role.getId());
        }

        int added =  roleMapper.saveRolePower(role.getRolePowerList());
        System.out.println(byId + updated + added);
        return byId + updated + added;
    }



}
